/*
 * IngredientManager.java
 *
 * Created on 30 octobre 2007, 18:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ifips5.nal.epizza.web;

import com.ifips5.nal.epizza.util.JDBCManager;
import com.ifips5.nal.epizza.web.model.Ingredient;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author Moi
 */
public class IngredientManager
{
    private BeanManager beanManager = null;
    private Ingredient currentIngredient = null;
    private List<Ingredient> ingredientList = new LinkedList();
    private Map<Integer, Ingredient> ingredientMap = new HashMap();
    
    
    
    
    
  public IngredientManager()
  {}

    
    /** Creates a new instance of IngredientManager */
    public IngredientManager(BeanManager beanManager)
    {   
        this.beanManager = beanManager;
        
    }

    public void updateIngredient(String id, String name, String stock)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("UPDATE ingredient SET name = \""+name+"\", stock="+stock+" WHERE idIngredient="+id);
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }    
        beanManager.updatePDF();
    }
    
    public void loadIngredient(String id)
    {
        String command = "SELECT * FROM ingredient WHERE idIngredient = "+id;
        int[] keyCol = {1};
        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            if(wrs.next())
            {
                this.currentIngredient = new Ingredient(wrs.getInt(1), wrs.getString(2), wrs.getInt(3));
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void loadIngredients()
    {
        String command = "SELECT * FROM ingredient";
       int[] keyCol = {1};

        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while(wrs.next())
            {
                Ingredient currentIngredient = new Ingredient(wrs.getInt(1), wrs.getString(2), wrs.getInt(3));
                this.ingredientMap.put(wrs.getInt(1), currentIngredient);
                this.ingredientList.add(currentIngredient);
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }        
    }


    public void deleteIngredient(String idIngredient)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("DELETE FROM ingredient WHERE idIngredient=\""+idIngredient+"\"");
            stmt.addBatch("DELETE FROM ingredient_has_pizza WHERE Ingredient_idIngredient="+idIngredient);
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        beanManager.updatePDF();
    }
    
    
    public void deleteIngredient(String[] delIngredients)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            
            for(int i = 0; i < delIngredients.length ; i++)
            {
                stmt.addBatch("DELETE FROM ingredient_has_pizza WHERE Ingredient_idIngredient="+delIngredients[i]);
                stmt.addBatch("DELETE FROM ingredient WHERE idIngredient="+delIngredients[i]);
                
            }

            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }    
        beanManager.updatePDF();
    }
    
    public void insertIngredient(String name)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("INSERT INTO ingredient(name, stock) VALUES ( \""+name+"\", 30)");
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        beanManager.updatePDF();
    }

    //////////////////////
    // Getters and Setters

    public BeanManager getBeanManager()
    {
        return beanManager;
    }

    public void setBeanManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }

    public Ingredient getCurrentIngredient()
    {
        return currentIngredient;
    }

    public void setCurrentIngredient(Ingredient currentIngredient)
    {
        this.currentIngredient = currentIngredient;
    }

    public List<Ingredient> getIngredientList()
    {
        return ingredientList;
    }

    public void setIngredientList(List<Ingredient> ingredientList)
    {
        this.ingredientList = ingredientList;
    }

    public Map<Integer, Ingredient> getIngredientMap()
    {
        return ingredientMap;
    }

    public void setIngredientMap(Map<Integer, Ingredient> ingredientMap)
    {
        this.ingredientMap = ingredientMap;
    }

    
}

